namespace MicroCloud.CodeGeneration.Database
{
    [Dependency(ServiceLifetime.Singleton, TryAdd = true)]
    public class CodeTemplateInitializer : SeedDataInitializerBase<CodeTemplate, Guid>
    {
        /// <summary>
        /// 初始化一个模板种子数据初始值设定项（<see cref="SeedDataInitializerBase{TEntity, TKey}"/>）的新实例
        /// </summary>
        public CodeTemplateInitializer(IServiceProvider rootProvider)
            : base(rootProvider)
        { }

        /// <summary>获取 种子数据初始化的顺序</summary>
        public override decimal OrderNo => 0;

        /// <summary>是否强制执行</summary>
        public override bool ForceSync => true;

        /// <summary>重写以提供要初始化的种子数据</summary>
        /// <returns></returns>
        protected override CodeTemplate[] SeedData(IServiceProvider provider)
        {
            return new[]
            {
                new CodeTemplate(){ Name = "cs_Entity", MetadataType = MetadataType.Entity, TemplateFile = "内置", Order = 1, IsSystem = true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/Core/Entities/{Entity.Name}Entity.g.cs" },
                new CodeTemplate(){ Name = "cs_InputDto", MetadataType = MetadataType.Entity, TemplateFile = "内置", Order = 2, IsSystem = true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/Core/Dtos/{Entity.Name}InputDto.g.cs"},
                new CodeTemplate(){ Name = "cs_OutputDto", MetadataType = MetadataType.Entity, TemplateFile = "内置", Order = 3, IsSystem = true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/Core/Dtos/{Entity.Name}OutputDto.g.cs"},

                new CodeTemplate(){ Name = "cs_AutoMapperConfiguration", MetadataType = MetadataType.Module, TemplateFile = "内置", Order = 4, IsSystem = true,IsOnce=true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/Core/AutoMapperConfiguration.cs" },
                new CodeTemplate(){ Name = "cs_EntityStore", MetadataType = MetadataType.Entity, TemplateFile = "内置", Order = 5, IsSystem = true,IsOnce=true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/Core/{Entity.Name}Store.cs" },

                new CodeTemplate(){ Name = "cs_EntityConfigurationBase", MetadataType = MetadataType.Entity, TemplateFile = "内置", Order = 6, IsSystem = true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/Database/Configurations/{Entity.Name}ConfigurationBase.g.cs" },
                new CodeTemplate(){ Name = "cs_EntityConfiguration", MetadataType = MetadataType.Entity, TemplateFile = "内置", Order = 7, IsSystem = true,IsOnce=true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/Database/Configurations/{Entity.Name}Configuration.cs" },

                new CodeTemplate(){ Name = "cs_ModuleContract", MetadataType = MetadataType.Module, TemplateFile = "内置", Order = 8, IsSystem = true,IsOnce=true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/I{Module.Name}Contract.cs" },
                new CodeTemplate(){ Name = "cs_ModuleService", MetadataType = MetadataType.Module, TemplateFile = "内置", Order = 9, IsSystem = true,IsOnce=true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/{Module.Name}Service.cs" },
                new CodeTemplate(){ Name = "cs_ModulePack", MetadataType = MetadataType.Module, TemplateFile = "内置", Order = 10, IsSystem = true,IsOnce=true, OutputFileFormat = "packs/{Project.NamespacePrefix}.{Module.Name}/{Module.Name}Pack.cs" },                

                new CodeTemplate(){ Name = "cs_ControllerBase", MetadataType = MetadataType.Module, TemplateFile = "内置", Order = 11, IsSystem = true,IsOnce=true, OutputFileFormat = "apis/{Project.NamespacePrefix}.Api/Areas/Admin/AdminControllerBase.cs" },
                new CodeTemplate(){ Name = "cs_Controller", MetadataType = MetadataType.Entity, TemplateFile = "内置", Order = 12, IsSystem = true, IsOnce = true, OutputFileFormat = "apis/{Project.NamespacePrefix}.Api/Areas/Admin/Controllers/{Module.Name}/{Entity.Name}Controller.cs"},
            };
        }

        /// <summary>重写以提供判断某个实体是否存在的表达式</summary>
        /// <param name="entity">要判断的实体</param>
        /// <returns></returns>
        protected override Expression<Func<CodeTemplate, bool>> ExistingExpression(CodeTemplate entity)
        {
            return m => m.Name == entity.Name;
        }
    }
}
